/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getUserInfo();
    var formData = {
        uid: uinfo.uid, role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function getHtml(d){
    var h = '', i = 0;
    for (; i < d.length; i ++) {
        h += '<table class="tbox" cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">人员名称</td>';
        h += '<td class="tdr">' + d[i].uname + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">所在部门</td>';
        h += '<td class="tdr">' + d[i].depart_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl">账号状态</td>';
        h += '<td class="tdr">' + d[i].status_name + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl"></td>';
        h += '<td class="tdr">';
        h += '<a href="javascript:;" onclick="edit(' + d[i].uid + ');">修改</a>';
        h += '<input type="hidden" id="status' + d[i].uid + '" value="' + d[i].status + '"/>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#tbody').html(h);
}


//编辑弹窗
function edit(v){
    var status = $('#status' + v).val();
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>状态设置</div>';
    h += '<select id="is_able">';
    if (status == 0) {
        h += '<option value="0" selected>正常</option>';
        h += '<option value="1">禁用</option>';
    } else {
        h += '<option value="0">正常</option>';
        h += '<option value="1" selected>禁用</option>';
    }
    h += '</select>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="modify(' + v + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//编辑保存
function modify(v){
    var is_able = $('#is_able').val();
    var formData = {
        uid: v, is_able: is_able,
    };
    //console.log(formData); return
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'staff/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}